package UDPSearchIPAndPort_TCP_Channel.server;


import UDPSearchIPAndPort_TCP_Channel.constants.TCPConstants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * TCPServer UDPProvider 在同一个端口
 *  UDPProvider 负责接收监听端口数据 打印输出 满足命令 回传 ip地址与port 用于searcher 端调用，创建client
 *  client通过ip与port 创建tcp连接，发送消息
 *
 *  tcpServer 接收到消息后 ，bufferedReader.readLine() 群发所有客户端tcpServer.broadcast(str);
 *
 */
public class Server {
    public static void main(String[] args) throws IOException {
        TCPServer tcpServer = new TCPServer(TCPConstants.PORT_SERVER);
        boolean isSucceed = tcpServer.start();
        if (!isSucceed) {
            System.out.println("Start TCP server failed!");
            return;
        }

        UDPProvider.start(TCPConstants.PORT_SERVER);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str;
        do {
            str = bufferedReader.readLine();
            //发送到所有客户端
            tcpServer.broadcast(str);
        } while (!"00bye00".equalsIgnoreCase(str));

        UDPProvider.stop();
        tcpServer.stop();
    }
}
